package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.sys.entity.*;
import io.renren.modules.sys.service.DormAreaService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.service.DormViolationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * @author Lizhiwei
 * @email 123@qq.com
 * @date 2022-10-30 00:38:52
 */
@RestController
@RequestMapping("sys/dormviolation")
public class DormViolationController extends AbstractController{
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private DormViolationService dormViolationService;
    @Autowired
    private DormAreaService dormAreaService;

    /**
     * 获取个人违规记录
     */
    @RequestMapping("/getByCurrentUserId")
//    @RequiresPermissions("sys:dormfare:info")
    public R getByCurrentUserId(){
        SysUserEntity user = getUser();
        List<DormViolationEntity> list = dormViolationService.list(new QueryWrapper<DormViolationEntity>().eq("user_id", user.getUserId()));
        List<DormViolationEntity> collect = list.stream().map((entity) -> {
            entity.setNum(user.getNum());
            entity.setRealName(user.getRealName());
            return entity;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:dormviolation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = dormViolationService.queryPage(params);
        List<DormViolationEntity> list = (List<DormViolationEntity>) page.getList();
        List<Object> list1 = list.stream().map((entity) -> {
            SysUserEntity user = sysUserService.getById(entity.getUserId());
            if (user==null){
                return R.error();
            }
            entity.setNum(user.getNum());
            entity.setRealName(user.getRealName());

            if (entity.getDormId() != null) {
                //获取真实宿舍号
                DormAreaEntity byId = dormAreaService.getById(entity.getDormId());
                if (byId!=null && byId.getName()!=null && !byId.getName().equals("")){
                    entity.setDormId(Integer.parseInt(byId.getName()));
                    //获取宿舍楼
                    DormAreaEntity byId1 = dormAreaService.getById(byId.getParentId());
                    entity.setSecondLevel(byId1.getName());
                    //获取宿舍区
                    DormAreaEntity byId2 = dormAreaService.getById(byId1.getParentId());
                    entity.setFirstLevel(byId2.getName());
                }

            }
            return entity;
        }).collect(Collectors.toList());
        page.setList(list1);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:dormviolation:info")
    public R info(@PathVariable("id") Integer id) {
        DormViolationEntity dormViolation = dormViolationService.getById(id);

        return R.ok().put("dormViolation", dormViolation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:dormviolation:save")
    public R save(@RequestBody DormViolationEntity dormViolation) {
        SysUserEntity entity = sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq("num", dormViolation.getNum()));
        if (entity == null) {
            return R.error();
        }
        if (!dormViolation.getRealName().equals(entity.getRealName())) {
            return R.error();
        }
        dormViolation.setDormId(entity.getDormId());
        dormViolation.setUserId(entity.getUserId().intValue());
        dormViolation.setCreateTime(new Date());
        dormViolationService.save(dormViolation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:dormviolation:update")
    public R update(@RequestBody DormViolationEntity dormViolation) {
        dormViolationService.updateById(dormViolation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:dormviolation:delete")
    public R delete(@RequestBody Integer[] ids) {
        dormViolationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
